package com.zhang.yygh.cmn.controller;


import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.model.cmn.Dict;
import com.zhang.yygh.cmn.service.DictService;
import com.zhang.yygh.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
@Api(tags = "字典表控制层")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 做文件上传：
     *  前端要求：
     *      1.必须是post请求
     *      2.设置type="file"
     *      3.form的encytype="multipart/form-data"
     *   后端要求：
     *   1.common-fileupload、commons-ioz
     *   2.springmvc.xml配置多部件解析器[id="multipart"]
     *   3.controller方法使用MultipartFile类型接收
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        dictService.upload(file);
        return R.ok();
    }

    @GetMapping("/download")
    public void download(HttpServletResponse httpServletResponse) throws IOException {
        dictService.download(httpServletResponse);
    }

    //根据所有省份查询所有市列表
    @ApiOperation(value = "根据父id查询子元素列表")
    @GetMapping("/childList/{pid}")
    public R getDictListByPid(@PathVariable(value = "pid") Long pid){
        List<Dict> dictList=dictService.getDictListByPid(pid);
        return R.ok().data("list",dictList);
    }

    //1.根据省、市、区编号查询对应的地址信息
    @GetMapping("/getName/{value}")
    public String getAddressNameByValue(@PathVariable(value = "value") Long value){
        return dictService.getAddressNameByValue("",value);
    }

    //2.根据医院等级编号和hoscode查询医院等级信息
    @GetMapping("/getName/{dictcode}/{value}")
    public String getAddressNameByValue(@PathVariable(value = "dictcode") String dictcode,
                                        @PathVariable(value = "value") Long value){
        return dictService.getAddressNameByValue(dictcode,value);
    }

    //根据dictcode查询所有省份信息
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }
}
